﻿using UnityEngine;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


public class XML<T> {

	#region -------------------LoadXml---------------------
	public static T LoadFromPath(string path)
	{
		try
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));
			using(FileStream stream = new FileStream(path, FileMode.Open))
			{
				return (T)serializer.Deserialize(stream);
			}
		}
		catch(Exception e)
		{
			Debug.Log(path);
			Debug.LogException(e);
		}
		
		return default(T);
	}
	
	public static List<T> LoadListFromPath(string path)
	{
		try
		{
			XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
			using(FileStream stream = new FileStream(path, FileMode.Open))
			{
				return (List<T>)serializer.Deserialize(stream);
			}
		}
		catch(Exception e)
		{
			Debug.Log(path);
			Debug.LogException(e);
		}
		
		return default(List<T>);
	}
	
	public static List<T> LoadListFromText(string text)
	{
		try
		{
			XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
			using(FileStream stream = new FileStream(text, FileMode.Open))
			{
				return (List<T>)serializer.Deserialize(new StreamReader(text));
			}
		}
		catch(Exception e)
		{
			Debug.LogException(e);
		}
		
		return default(List<T>);
	}
	
	#endregion -------------------LoadXml---------------------
	
	#region -------------------SaveXml---------------------
	
	public static bool SaveAtPath(T obj, string path)
	{
		try
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));
			using(FileStream stream = new FileStream(path, FileMode.Create))
			{
				TextWriter writer = new StreamWriter(stream, new UTF8Encoding());
				serializer.Serialize(writer, obj);
			}
		}
		catch(Exception e)
		{
			Debug.Log(path);
			Debug.LogException(e);
			return false;
		}
		
		return true;
	}
	
	public static bool SaveListAtPath(List<T> obj, string path)
	{
		try
		{
			XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
			using(FileStream stream = new FileStream(path, FileMode.Create))
			{
				TextWriter writer = new StreamWriter(stream, new UTF8Encoding());
				serializer.Serialize(writer, obj);
			}
		}catch(Exception e)
		{
			Debug.LogError(e);
			return false;
		}
		
		return true;
	}
	
	#endregion -------------------SaveXml---------------------
}

public class XML<Tkey, Tvalue>
{
	public static Dictionary<Tkey, Tvalue> LoadFromPath(string path)
	{
		XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<Tkey, Tvalue>));
		using(FileStream stream = new FileStream(path, FileMode.Open))
		{
			return (Dictionary<Tkey, Tvalue>)serializer.Deserialize(stream);
		}
	}
	
	public static Dictionary<Tkey, Tvalue> LoadFromText(string text)
	{
		XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<Tkey, Tvalue>));
		return (Dictionary<Tkey, Tvalue>)serializer.Deserialize(new StreamReader(text));
	}
	
	public static bool SaveAtPath(Dictionary<Tkey, Tvalue> obj, string path)
	{
		try
		{
			XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<Tkey, Tvalue>));
			using(FileStream stream = new FileStream(path, FileMode.Create))
			{
				TextWriter writer = new StreamWriter(stream, new UTF8Encoding());
				serializer.Serialize(writer, obj);
			}
		}
		catch(Exception e)
		{
			Debug.Log(path);
			Debug.LogException(e);
			return false;
		}
		
		return true;
	}
}
